

namespace Stee.CAP8.MessageBus.Kafka.Utility
{
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Linq;
    using Microsoft.Extensions.Configuration;
    using NLog;

    /// <summary>
    /// Helper for reading configurations from different configuration file types (*.config, *.json)
    /// </summary>
    public static class ConfigurationHelper
    {
        private readonly static Logger Logger = LogManager.GetCurrentClassLogger();

        public const string ConfigurationFileJSON = "appsettings.json";

        public const string MessageBusPublishConfig = "MessageBusPublish";

        public const string MessageBusSubscribeConfig = "MessageBusSubscribe";


        private readonly static IConfigurationRoot ConfigurationRoot;


        /// <summary>
        /// Initializes the ConfigurationHelper static class
        /// </summary>
        static ConfigurationHelper()
        {
            ConfigurationRoot = new ConfigurationBuilder()
                .AddJsonFile(ConfigurationFileJSON, false)
                .Build();
        }

        /// <summary>
        /// Read configuration fron JSON file with specified sector.
        /// If keys are duplicated in JSON file, the last key will over-write previous entries. 
        /// </summary>
        /// <param name="configSector">Sector name</param>
        /// <returns>Return collection mapping of configurations</returns>
        public static Dictionary<string, string> ReadJSONConfiguration(string configSector)
        {
            IConfigurationSection sectionMessageBus = ConfigurationRoot.GetSection(configSector);
            bool exists = sectionMessageBus.Exists();
            IList<IConfigurationSection> configSection = sectionMessageBus.GetChildren().ToList();

            Dictionary<string, string> configs = new Dictionary<string, string>();

            foreach (var item in configSection)
            {
                string key = item.Key.Trim();
                string value = item.Value.Trim();

                configs[key] = value;
            }

            return configs;
        }


        /// <summary>
        /// Read configuration from application config file with specified sector
        /// </summary>
        /// <param name="configSector">Sector name</param>
        /// <returns>Return collection mapping of configurations</returns>
        public static Dictionary<string, string> ReadApplicationConfiguration(string configSector)
        {
            NameValueCollection messageBusConfig = ConfigurationManager.GetSection(configSector) as NameValueCollection;

            if (messageBusConfig == null)
            {
                string message = $"System cannot parse configuration: {configSector}. Please check configuration file.";
                Logger.Fatal(message);
                throw new ConfigurationErrorsException(message);
            }

            Dictionary<string, string> configurations = new Dictionary<string, string>();

            foreach (string key in messageBusConfig.AllKeys)
            {
                //// Check the keys for front & back whitespaces
                string newKey = key.Trim();

                if (!string.IsNullOrEmpty(newKey) && !configurations.ContainsKey(newKey))
                {
                    configurations[newKey] = messageBusConfig[key];
                }
            }

            return configurations;
        }
    }
}
